package com.easylinkin.linkappapi.deviceattribute.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.device.entity.Device;
import com.easylinkin.linkappapi.device.service.DeviceService;
import com.easylinkin.linkappapi.deviceattribute.entity.DeviceAttribute;
import com.easylinkin.linkappapi.deviceattribute.mapper.DeviceAttributeMapper;
import com.easylinkin.linkappapi.deviceattribute.service.DeviceAttributeService;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import com.easylinkin.linkappapi.security.entity.LinkappUser;

import java.util.Collections;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author TongJie
 * @since 2020-04-15
 */
@Service
public class DeviceAttributeServiceImpl extends ServiceImpl<DeviceAttributeMapper, DeviceAttribute> implements DeviceAttributeService {

    @Resource
    LinkappUserContextProducer linkappUserContextProducer;

    @Resource
    private DeviceService deviceService;

    @Override
    public List<DeviceAttribute> getAll(DeviceAttribute deviceAttribute) {
        Assert.notNull(deviceAttribute, "参数为空");
        LinkappUser current = linkappUserContextProducer.getCurrent();
        if (current != null) {
            deviceAttribute.setTenantId(current.getTenantId());
        }
        return baseMapper.getAll(deviceAttribute);
    }

    @Override
    public List<DeviceAttribute> getAll2(DeviceAttribute deviceAttr) {
        if (deviceAttr == null) {
            return null;
        }
        QueryWrapper<DeviceAttribute> qw = new QueryWrapper();
        if (ObjectUtils.isNotEmpty(deviceAttr.getId())) {
            qw.eq("id", deviceAttr.getId());
        }
        if (ObjectUtils.isNotEmpty(deviceAttr.getDeviceUnitId())) {
            qw.eq("device_unit_id", deviceAttr.getDeviceUnitId());
        }
        if (ObjectUtils.isNotEmpty(deviceAttr.getIdentifier())) {
            qw.eq("identifier", deviceAttr.getIdentifier());
        }
        if (ObjectUtils.isNotEmpty(deviceAttr.getVersion())) {
            qw.eq("version", deviceAttr.getVersion());
        }
        qw.orderByDesc("create_time");

        return list(qw);
    }

    @Override
    public List<DeviceAttribute> getDeviceAttributesByDeviceCode(String deviceCode) {
        //获取当前设备在当前项目下的版本
        Device device = new Device();
        device.setCode(deviceCode);
        List<Device> devices = deviceService.selectDevices(device);
        if (CollectionUtil.isEmpty( devices)){
            return null;
        }
        String deviceUnitId = devices.get(0).getDeviceUnitId();
        DeviceAttribute deviceAttribute = new DeviceAttribute();
        deviceAttribute.setDeviceUnitId(deviceUnitId);
        return getAll(deviceAttribute);
    }
}
